﻿using KhoaLuan.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Web;
using System.Web.Mvc;

namespace KhoaLuan.Controllers
{
    public class HomeController : Controller
    {
        LuanVanEntities db = new LuanVanEntities();
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult Model_tuyendung()
        {
            var model = new Module();
            ViewBag.Tuyendung  = db.Modules
                .Where(m => m.position == "trangchu" && m.Name=="tuyendung" && m.Status == true)
                ;
            return PartialView("_TuyenDung",model);
        }
        public ActionResult Model_Facebook()
        {
            var model = new Module();
            ViewBag.Facebook = db.Modules
                .Where(m => m.position == "trangchu" && m.Name == "facebook" && m.Status == true)
                ;
            return PartialView("_Module_Facebook", model);
        }
        public ActionResult Model_LienHe()
        {
            var model = new Module();
            ViewBag.LienHe = db.Modules
                .Where(m => m.position == "trangchu" && m.Name == "lienhe" && m.Status == true)
                ;
            return PartialView("_FormLienHe", model);
        }
        public ActionResult Model_BanDo()
        {
            var model = new Module();
            ViewBag.BanDo = db.Modules
                .Where(m => m.position == "trangchu" && m.Name == "bando" && m.Status == true)
                ;
            return PartialView("_BanDo", model);
        }
        public ActionResult Model_ForShop()
        {
            var model = new Module();
            ViewBag.ForShop = db.Modules
                .Where(m => m.position == "trangchu" && m.Name == "forshop" && m.Status == true)
                ;
            return PartialView("_ForShop", model);
        }
        public ActionResult Model_Spnoibat()
        {
            var model = new Module();
            ViewBag.Spnoibat = db.Modules
                .Where(m => m.position == "trangchu" && m.Name == "sanphamnoibat" && m.Status == true)
                ;
            return PartialView("_Sanphamnoibat", model);
        }
        public ActionResult Model_Dachungnhan()
        {
            var model = new Module();
            ViewBag.Dachungnhan = db.Modules
                .Where(m => m.position == "trangchu" && m.Name == "dachungnhan" && m.Status == true)
                ;
            return PartialView("_DachungNhan", model);
        }

       
       
        public ActionResult MenuCapMot()
        {
            var model = new Menu();
            ViewBag.Capmot = db.Menus
                .Where(m => m.Level == 2 && m.Status == true);
            return PartialView("_MenuDaCap", model);
        }

        [ValidateInput(false)]
        [ValidateAntiForgeryToken]
        public ActionResult Send(MailInfo model)
        {
            try
            {
                  // Tạo thư
                var mail = new MailMessage();
                mail.From = new MailAddress("ForShop <" + model.Sender + ">");
                mail.To.Add("trinhdinhphuc1006@gmail.com");
                mail.Subject = model.Subject;
                mail.Body = model.Content;

                mail.ReplyToList.Add(model.Sender);
                mail.IsBodyHtml = true;

                // Kết nối SmtpServer
                var smtp = new SmtpClient("smtp.gmail.com", 25);
                smtp.EnableSsl = true;
                smtp.Credentials = new NetworkCredential("kentphp2@gmail.com", "songlong");

                // Gửi thư
                smtp.Send(mail);
                ViewBag.Message = " Gủi Mail Thành Công";
            }
            catch(Exception ex)
            {
                ViewBag.Message = "Gủi Mail Thất Bại Vui lòng kiểm tra lại thông tin";
            }
          

            return View("Index");
        }
        public ActionResult Categordy()
        {
            var model = db.Categories;
            return View("Index", model);

        }
        public ActionResult CategoryMenuLeft()
        {
            var model = db.Categories;
            return PartialView("_MenuLeft", model);

        }
        public ActionResult BananerShow()
        {
            var model = db.Bananers
                .Where(b=>b.Status == true);
            return PartialView("_Shileshow", model);
        }
        public ActionResult ListByCategory(int Id)
        {
            var model = db.Products
                .Where(p => p.CategoryId == Id);
            return View("Index", model);
        }
        public ActionResult Shileshowbananer(int Id)
        {
            var model = db.Bananers;
            return PartialView("_Shileshow", model);
        }
        /* sản phẩm mới*/
        public ActionResult NGAUNHIEN(String Id)
        {

            var model = new Product();
            ViewBag.Spngaunhien = db.Products
                        .Where(p => p.Latest == true)
                        .OrderBy(p => Guid.NewGuid())
                        .Take(5)
                        .ToList()
                        ;
            return PartialView("_SanPhamMoi", model);
        }
        /* sản phẩm ngẫu nhiên */
        public ActionResult MOI(String Id)
        {

            var model = new Product();
            ViewBag.Spmoi = db.Products
                        .Where(p => p.Latest == true)
                        .Take(5)
                        ;
            return PartialView("_SanPhamMoi", model);
        }

        /* sản phẩm bán chạy*/
        public ActionResult BAN_CHAY(String Id)
        {

            var model = new Product();
            ViewBag.Spbanchay = db.Products
                       .Where(p => p.OrderDetails.Count > 0)
                       .OrderByDescending(p => p.OrderDetails.Sum(d => d.UnitPrice * d.Quantity))
                       .OrderBy(p => Guid.NewGuid())
                       .Take(10)
                       .ToList();

            return PartialView("_SanPhamBanChay", model);
        }
        public ActionResult GIAM_GIA(String Id)
        {

            var model = new Product();
            ViewBag.Spgiamgia = db.Products
                        .Where(p => p.Discount > 0)
                        .OrderByDescending(p => p.Discount)
                        .OrderBy(p => Guid.NewGuid())
                        .Take(10)
                        .ToList();

            return PartialView("_SanPhamGiamGia", model);
        }
        /*sản phẩm xem nhiều */
        public ActionResult XEM_NHIEU(String Id)
        {

            var model = new Product();
            ViewBag.Spxemnhieu = db.Products
                        .Where(p => p.Views > 0)
                        .OrderByDescending(p => p.Views)
                        .OrderBy(p => Guid.NewGuid())
                        .ToList();

            return PartialView("_SanPhamXemNhieu", model);
        }
    }
}